import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import il.ac.braude.sona.constants.TestingParamKit;
import il.ac.braude.sona.control.ExtensionsController;
import il.ac.braude.sona.control.ViewRequestController;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.entities.ExtensionRequest;
import il.ac.braude.sona.gui.SONAFrame;
import il.ac.braude.sona.ocsf.client.ConnectToClientForTesting;
import fit.ActionFixture;


public class ExtensionsDeclineFitTester extends ActionFixture {
	private ViewRequestController viewController= new ViewRequestController();
	private ExtensionsController extController = new ExtensionsController();
	private ConnectToClientForTesting connection= new ConnectToClientForTesting();
	private TestingParamKit TestKit=TestingParamKit.getTestingKit();
	private ExtensionRequest request;
	private ChangeRequest chreq;
	private String oldDate = null,newDate = null,explanation = "";
	private String currentRid;
	
	public void startExtensionsDeclineFitTester() {

	}
    public void getFinishDate() {
    	viewController.execViewChangeRequest();
		TestKit.waitForNotify();
    	viewController.viewReqById(currentRid);
    	TestKit.waitForNotify();
    	if (oldDate == null)
    		oldDate = viewController.getFields().get("Estimated stage finish:");
    	else
    		newDate = viewController.getFields().get("Estimated stage finish:");
    }
	public void startProgramWithServer(String host) {
		showTestingMessage();
		TestKit.startTestMode();
		connection.setHost(host);
		ArrayList<Object> values=new ArrayList<Object>();
		values.add("sasha");
		values.add("1234");
		connection.connectToClient(values);
	}
	public void requestIdToExtend(String Id) {
		currentRid = Id;
		viewController.viewReqFromMssg(currentRid);
		TestKit.waitForNotify();
		chreq = viewController.getTestingChangeReq();
	}
	public void checkEmptyExplanation() {
		ArrayList<Object> values = new ArrayList<Object>();
		values.add(explanation);
		extController.execRequestExtension(values);
		TestKit.waitForNotify();
	}
	public void requestExtension(String days) {
		ArrayList<Object> values = new ArrayList<Object>();
		explanation = "fit test";
		values.add(days);
		values.add(explanation);
		values.add(chreq.getID());
		values.add(chreq.getValue("Status"));
		values.add("Submit");
		extController.execRequestExtension(values);
		TestKit.waitForNotify();
	}
	public void handleDeclineDecision() {
		extController.execViewExtensionlist();
		TestKit.waitForNotify();
		request = extController.values.get(extController.index());
		extController.execApproveRequestHandle(request.values, "Decline");
		TestKit.waitForNotify();
	}
	public String extensionDecision() {
		return TestKit.extensionDecision;
	}
	public boolean checkDatabase() {
		return TestKit.inDatabase;
	}
	public boolean checkExtensionPremission() {
		return TestKit.extensionPerm;
	}
	
	public boolean checkDateChange() {
	    return !(oldDate.equals(newDate));
	}
	

	public boolean mailSent() {
		return TestKit.mailSent;
	}
	public String getOldDate() {
		return oldDate;
	}
	public String getNewDate() {
		return newDate;
	}
	private void showTestingMessage(){
		SONAFrame frame = null;
		JDialog dialog = new JDialog(frame, false); // Sets its owner but makes it non-modal 
		JOptionPane optionPane = new JOptionPane("Please dont press on poped windows, wait until test finish"); // Same arguments as in JOptionPane.showMessageDialog(...)
		dialog.getContentPane().add(optionPane); // Adds the JOptionPane to the dialog
		dialog.pack(); // Packs the dialog so that the JOptionPane can be seen
		dialog.setVisible(true); // Shows the dialog
	}

}
